﻿#include "PyFITKMaterialsFoams.h"
#include "FITKAcousticsPhysics.h"
#include "FITKAcousticsAbsMaterial.h"
#include "FITKAcousticsMaterialsFoam.h"
namespace Acoustics {
    PyFITKMaterialsFoams::PyFITKMaterialsFoams(const QString & name) :
        Acoustics::PyFITKMaterialsBase(name)
    {
    }

    bool PyFITKMaterialsFoams::setDensity(double density)
    {
        if (!_material|| density<0)return false;

        FITKAcousticsMaterialsFoam *foam = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_material);
        if (!foam)return false;

        foam->setDensity(density);
        return true;
    }

    bool PyFITKMaterialsFoams::setModulus(double shear, double young, double poissonRatio)
    {
        if (!_material)return false;
        if (shear < 0 || young < 0 || poissonRatio < 0)return false;

        FITKAcousticsMaterialsFoam *foam = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_material);
        if (!foam)return false;

        foam->setshearModulus(shear);
        foam->setTensileModulus(young);
        foam->setPoissonRatio(poissonRatio);
        return true;
    }

    bool PyFITKMaterialsFoams::setOtherProp(double flowResistivity, double porosity, double tortuosity, double viscousLength, double thermalLength, double lossFactor)
    {
        if (!_material)return false;
        if (flowResistivity < 0 || porosity < 0 || tortuosity < 0|| viscousLength < 0 || thermalLength < 0 || lossFactor < 0)return false;

        FITKAcousticsMaterialsFoam *foam = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_material);
        if (!foam)return false;

        foam->setFlowResistivity(flowResistivity);
        foam->setPorosity(porosity);
        foam->setTortuosity(tortuosity);
        foam->setViscousLength(viscousLength);
        foam->setThermalLength(thermalLength);
        foam->setLossFactor(lossFactor);
        return true;
    }

    bool PyFITKMaterialsFoams::createMaterial(const QString & name)
    {
        if (name == "")return false;
        Acoustics::FITKAcousticsPhysics* phy = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
        if (phy == nullptr) return false;
        Acoustics::FITKAcousticsMaterialManager* matMgr = phy->getMaterialManager();
        if (matMgr == nullptr) return false;
        if (matMgr->getDataByName(name))return false;

        FITKAcousticsMaterialsFoam * foam = new FITKAcousticsMaterialsFoam();
        foam->setDataObjectName(name);
        _material = foam;
        matMgr->appendDataObj(_material);

        return true;
    }
}